package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.entity.Employee;
import com.example.demo.service.EmployeeService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    //增添数据
    @PostMapping("/add")
    public Result add(@RequestBody Employee employee){
        employeeService.add(employee);
        return Result.success();
    }


    //更新数据
    @PutMapping("/update")
    public Result update(@RequestBody Employee employee){
        employeeService.update(employee);
        return Result.success();
    }

    //删除单个数据
    @DeleteMapping("/deletById/{id}")
    public Result deletById(@PathVariable Integer id){
        employeeService.deletById(id);
        return Result.success();
    }

    //查询所有数据
    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Employee> list = employeeService.selectAll();
        return Result.success(list);
    }

    //查询单个数据
    @GetMapping("/selectById/{id}/{age}")///selectById/xxx
    public Result selectById(@PathVariable Integer id,@PathVariable Integer age){
        Employee employee = employeeService.selectById(id);
        return Result.success(employee);
    }


    //分页查询
    //pageNum: 当前页码
    //pageSize: 每页的个数
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "9") Integer pageSize){
        PageInfo<Employee> pageInfo = employeeService.selectPage(pageNum, pageSize);
        return Result.success(pageInfo);
    }

    /*查询多个数据
    @GetMapping("/selectOne")///selectOne?id=xxx    不可带空格
    public Result selectOne(@RequestParam Integer id,@RequestParam(required = false) Integer age){
        Employee employee = employeeService.selectById(id);
        return Result.success(employee);
    }


    @GetMapping("/selectList")///selectOne?id=xxx    不可带空格
    public Result selectList(Employee employee){
        List<Employee> list = employeeService.selectList(employee);
        return Result.success(list);
    }


     */

}
